import { getInfo, getConfig, updateConfig } from '@/services/config';

export default {
    namespace: 'config',

    state: {
        info: {},
        config: {},
    },

    effects: {
        *fetchInfo({ payload, callback }, { call, put }) {
            const response = yield call(getInfo, payload);
            yield put({
                type: 'info',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },

        *fetch({ payload, callback }, { call, put }) {
            const response = yield call(getConfig, payload);
            yield put({
                type: 'config',
                payload: response,
            });
            if (response && callback) {
                callback(response.data);
            }
        },
        *update({ payload, callback }, { call, put }) {
            const response = yield call(updateConfig, payload);
            if (response && callback) {
                callback(response);
            }
        },
    },

    reducers: {
        info(state, action) {
            return {
                ...state,
                info: action.payload.data
            };
        },
        config(state, action) {
            return {
                ...state,
                config: action.payload.data
            };
        }
    },
};
